// Cookie管理工具类
export interface CookiePreferences {
  necessary: boolean;
  analytics: boolean;
  marketing: boolean;
  functional: boolean;
}

export class CookieManager {
  // 检查用户是否已经设置Cookie偏好
  static hasConsent(): boolean {
    return localStorage.getItem('cookie-consent') !== null;
  }

  // 获取用户的Cookie偏好
  static getPreferences(): CookiePreferences {
    const preferences = localStorage.getItem('cookie-preferences');
    if (preferences) {
      return JSON.parse(preferences);
    }
    
    // 默认偏好（仅必要Cookie）
    return {
      necessary: true,
      analytics: false,
      marketing: false,
      functional: false
    };
  }

  // 设置Cookie偏好
  static setPreferences(preferences: CookiePreferences): void {
    localStorage.setItem('cookie-preferences', JSON.stringify(preferences));
    localStorage.setItem('cookie-consent', 'custom');
    
    // 根据偏好启用/禁用相应的追踪
    this.applyPreferences(preferences);
  }

  // 接受所有Cookie
  static acceptAll(): void {
    const allAccepted: CookiePreferences = {
      necessary: true,
      analytics: true,
      marketing: true,
      functional: true
    };
    
    localStorage.setItem('cookie-preferences', JSON.stringify(allAccepted));
    localStorage.setItem('cookie-consent', 'accepted');
    
    this.applyPreferences(allAccepted);
  }

  // 仅接受必要Cookie
  static acceptNecessaryOnly(): void {
    const necessaryOnly: CookiePreferences = {
      necessary: true,
      analytics: false,
      marketing: false,
      functional: false
    };
    
    localStorage.setItem('cookie-preferences', JSON.stringify(necessaryOnly));
    localStorage.setItem('cookie-consent', 'necessary-only');
    
    this.applyPreferences(necessaryOnly);
  }

  // 清除所有Cookie设置
  static clearConsent(): void {
    localStorage.removeItem('cookie-consent');
    localStorage.removeItem('cookie-preferences');
    
    // 清除可能设置的追踪Cookie
    this.clearTrackingCookies();
  }

  // 应用Cookie偏好
  private static applyPreferences(preferences: CookiePreferences): void {
    // 分析Cookie
    if (preferences.analytics) {
      // 这里可以初始化Google Analytics或其他分析工具
      console.log('Analytics cookies enabled');
      this.enableAnalytics();
    } else {
      console.log('Analytics cookies disabled');
      this.disableAnalytics();
    }

    // 营销Cookie
    if (preferences.marketing) {
      console.log('Marketing cookies enabled');
      this.enableMarketing();
    } else {
      console.log('Marketing cookies disabled');
      this.disableMarketing();
    }

    // 功能Cookie
    if (preferences.functional) {
      console.log('Functional cookies enabled');
      this.enableFunctional();
    } else {
      console.log('Functional cookies disabled');
      this.disableFunctional();
    }
  }

  // 启用分析追踪
  private static enableAnalytics(): void {
    // 示例：Google Analytics初始化
    // gtag('config', 'GA_MEASUREMENT_ID');
  }

  // 禁用分析追踪
  private static disableAnalytics(): void {
    // 示例：禁用Google Analytics
    // gtag('config', 'GA_MEASUREMENT_ID', { send_page_view: false });
  }

  // 启用营销追踪
  private static enableMarketing(): void {
    // 示例：Facebook Pixel等营销工具
  }

  // 禁用营销追踪
  private static disableMarketing(): void {
    // 清除营销相关的Cookie
  }

  // 启用功能Cookie
  private static enableFunctional(): void {
    // 启用聊天工具、偏好设置等功能
  }

  // 禁用功能Cookie
  private static disableFunctional(): void {
    // 禁用非必要的功能Cookie
  }

  // 清除追踪Cookie
  private static clearTrackingCookies(): void {
    // 清除所有非必要的Cookie
    const cookies = document.cookie.split(';');
    
    for (const cookie of cookies) {
      const eqPos = cookie.indexOf('=');
      const name = eqPos > -1 ? cookie.substr(0, eqPos).trim() : cookie.trim();
      
      // 保留必要的Cookie，清除其他的
      if (!this.isNecessaryCookie(name)) {
        document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/`;
      }
    }
  }

  // 判断是否为必要Cookie
  private static isNecessaryCookie(cookieName: string): boolean {
    const necessaryCookies = [
      'cookie-consent',
      'cookie-preferences',
      'JSESSIONID',
      'PHPSESSID'
      // 添加其他必要的Cookie名称
    ];
    
    return necessaryCookies.some(necessary => 
      cookieName.toLowerCase().includes(necessary.toLowerCase())
    );
  }

  // 获取同意状态的描述
  static getConsentStatus(): string {
    const consent = localStorage.getItem('cookie-consent');
    switch (consent) {
      case 'accepted':
        return 'All cookies accepted';
      case 'necessary-only':
        return 'Essential cookies only';
      case 'custom':
        return 'Custom settings';
      default:
        return 'Not set';
    }
  }
}
